import 'package:pornhub_app/components/app_bg_view.dart';
import 'package:pornhub_app/components/easy_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/generate/app_image_path.dart';

import '../../../assets/styles.dart';
import '../../../components/common_permission_alert.dart';
import '../../../routes/routes.dart';
import '../../../utils/color.dart';
import '../frontpage/controller/edit_userinfo_controller.dart';

/// 编辑名字
class EdituserNamePage extends StatelessWidget {
  const EdituserNamePage({super.key});

  @override
  Widget build(BuildContext context) {
    final bgColor = Styles.color.bgColor;

    return Scaffold(
      backgroundColor: bgColor,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        iconTheme: const IconThemeData(color: Colors.white),
        title: Text("编辑个人资料",
            textAlign: TextAlign.center,
            style: TextStyle(
              color: COLOR.hexColor('#ffffff'),
              fontSize: 16.sp,
              fontWeight: FontWeight.w500,
            )),
        centerTitle: true,
      ),
      body: const EditUserNameChild(),
    );
  }
}

class EditUserNameChild extends StatefulWidget {
  const EditUserNameChild({super.key});

  @override
  State<StatefulWidget> createState() => EditUserNameChildState();
}

class EditUserNameChildState extends State<EditUserNameChild> {
  TextEditingController contentController = TextEditingController();

  final controller = Get.find<EditUserInfoController>();

  void onSaveClick() {
    if (contentController.text.isEmpty) {
      EasyToast.show('请输入名字');
      return;
    }
    if (!controller.us.isVIP) {
      permission_alert(Get.context!, desc: "您还不是会员,会员才可以修改！", oktitle: "去开通",
          okAction: () {
        Get.toNamed(Routes.vip);
      });
      return;
    }
    controller.postModifyInfo(name: contentController.text);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        padding:
            EdgeInsets.only(left: 10.w, right: 10.w, top: 10.w, bottom: 30.w),
        height: double.maxFinite,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    "我的名字",
                    style: TextStyle(
                      fontSize: 15.sp,
                      color: Colors.white,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 14.w)),
                  TextField(
                    controller: contentController,
                    maxLength: 8,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 15.sp,
                    ),
                    decoration: InputDecoration(
                      hintText: '${controller.userInfo.nickName}',
                      helperText: '名字30天内可修改1次，名字最多8个字',
                      hintStyle: TextStyle(
                        color: Colors.white70,
                        fontSize: 15.sp,
                      ),
                      helperStyle: TextStyle(
                        color: COLOR.color_B940FF,
                        fontSize: 13.sp,
                      ),
                      focusedBorder: UnderlineInputBorder(
                        borderSide: BorderSide(
                          color: COLOR.hexColor('#F0F0F0'),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            InkWell(
              onTap: () => onSaveClick(),
              child: AppBgView(
                width: double.maxFinite,
                height: 40.w,
                imagePath: AppImagePath.mine_img_exchange_vip_bg,
                fit: BoxFit.fill,
                child: Flex(
                  direction: Axis.horizontal,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      "保存",
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: Colors.white,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ));
  }
}
